<template>
  <div class="detail-modal fullscreen" style="height: auto">
    <a-button
      class="detail-export-btn"
      type="primary"
      @click="handleExport"
      :disabled="isGenerate == 0"
      :title="isGenerate == 0 ? '报告未生成，不能导出' : ''"
      >导出</a-button
    >
    <detail-iframe />
  </div>
</template>

<script>
import exportHandle from '@/utils/export-util/pdf-canvas'
import * as caseApi from '@/api/modules/case.js'
import { downLoadMix } from '@/utils/export-util/util'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import DetailIframe from './detail-iframe.vue'
export default {
  components: {
    DetailIframe,
  },
  data() {
    return {
      isGenerate: null,
      reportFileAddress: null,
    }
  },
  created() {
    this.isGenerate = this.$route.query.isGenerate
    this.reportFileAddress = this.$route.query.reportFileAddress
  },
  methods: {
    getUrl() {
      const domain = window._CONFIG['domianURL']
      const id = this.$route.query.id
      const token = this.$ls.get(ACCESS_TOKEN)
      const reportDate = this.$route.query.reportDate
      return `/case/report/detail-iframe?token=${token}&reportDate=${reportDate}`
    },
    handleExport() {
      // window.open(this.getUrl())
      let that = this
      let params = {
        fileList: [this.reportFileAddress],
      }
      if (params.fileList.length === 0) {
        this.$message.warn('导出文件不存在！')
        return
      }
      this.$confirm({
        title: `是否确认导出报告？`,
        okText: '确定',
        onOk() {
          // that.onDownload()
          caseApi.exportReport(params).then(async (res) => {
            if (res && res.size > 0) {
              const fileName = '案件分析报告.zip'
              await downLoadMix(res, fileName)
            } else {
              that.$message.warn('导出失败！')
            }
          })
        }
      })
    },
    onDownload() {
      const reportDate = this.$route.query.reportDate
      this.download(`#reportDetails-${reportDate}`, '案件分析报告')
    },
    // 下载PDF
    download(DomID, filename) {
      let refDom = document.querySelector(DomID)
      this.$nextTick(() => {
        exportHandle.exportPDF(refDom, filename)
      })
    },
  },
}
</script>
<style lang="less" scoped>
.detail-modal {
  background-color: #fff;
  .detail-export-btn {
    margin: 24px 0 0 24px;
  }
}
</style>

